package com.apobates.forum.toro.core;

import java.io.Serializable;

/**
 * 词语[/属性]的描述性角注
 */
public class Corner implements Serializable,Comparable<Corner> {
    // 是否必须
    private final boolean required;
    // 类型名称
    private final String typed;
    // 不是必须时的默认值
    private final String defaultVal;
    // 分隔符
    private final String delimiter;
    // 顺序
    private final int ranking;

    public Corner(boolean required, String typed, String defaultVal, String delimiter, int sequence) {
        this.required = required;
        this.typed = typed;
        this.defaultVal = defaultVal;
        this.delimiter = delimiter;
        this.ranking = sequence;
    }

    public Corner(String typed, String delimiter, int sequence) {
        this(true, typed, null, delimiter, sequence);
    }

    private Corner(Builder builder) {
        this(builder.required, builder.typed, builder.defaultVal, builder.delimiter, builder.ranking);
    }

    public static Builder sequence(int number){
        return Builder.getInstance(number);
    }

    public boolean isRequired() {
        return required;
    }

    public String getTyped() {
        return typed;
    }

    public String getDefaultVal() {
        return defaultVal;
    }

    public String getDelimiter() {
        return delimiter;
    }

    public int getRanking() {
        return ranking;
    }

    @Override
    public int compareTo(Corner o) {
        return this.ranking > o.ranking?1:-1;
    }

    @Override
    public String toString() {
        return "Corner{" +
                "required=" + required +
                ", typed='" + typed + '\'' +
                ", defaultVal='" + defaultVal + '\'' +
                ", delimiter='" + delimiter + '\'' +
                ", ranking=" + ranking +
                '}';
    }

    public static class Builder{
        private boolean required;
        // 类型名称
        private String typed;
        // 不是必须时的默认值
        private String defaultVal;
        // 分隔符
        private String delimiter;
        private int ranking;
        private Builder(int sequence) {
            this.required = true;
            this.defaultVal = null;
            this.ranking = sequence;
            this.typed = "java.lang.String";
        }

        /**
         * 返回一个构造实例
         * @param sequence 序列数值.越小越靠前
         * @return
         */
        public static Builder getInstance(int sequence){
            return new Builder(sequence);
        }

        /**
         * 是否是必需的. 默认为: true, 可选时需要手动调用
         * @param required
         * @return
         */
        public Builder require(boolean required){
            this.required = required;
            return this;
        }

        /**
         * 在可选时的默认值. 若require是必需的无需调用
         * @param defaultVal
         * @return
         */
        public Builder give(String defaultVal){
            this.defaultVal = defaultVal;
            return this;
        }

        /**
         * 词语[/属性]对应的java类型. 默认为: java.lang.String
         * @param typed 基础类型写类型名称. 例:int, 包装类写类全名: java.lang.Integer
         * @return
         */
        public Builder type(String typed){
            this.typed = typed;
            return this;
        }

        /**
         * 使用内置的类型枚举.
         * @param typeEnum 只限:String,8种基础类型及其对应的包装类
         * @return
         */
        public Builder type(TypeEnum typeEnum){
            this.typed = typeEnum.getType();
            return this;
        }
        /**
         * 分隔[/边缘]符号. 用于界定起始范围, 上一个分隔符号不能与下一个分隔符号一样
         * @param delimiter 分隔[/边缘]符号
         * @return
         */
        public Builder edge(String delimiter){
            this.delimiter = delimiter;
            return this;
        }

        /**
         * 构造Corner实例
         * @return
         */
        public Corner build(){
            return new Corner(this);
        }
    }
}
